/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanosn.coffeemaker;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * TODEV: Validate user input using regexp for each entry.
 *
 * @author selkhateeb
 */
public class Settings
{

    private static Properties SETTINGS;
    private static String DATABASE_CONNECTION_STRING;


    static
    {
        // Read properties file.
        SETTINGS = new Properties();
        try
        {
            String settingsFileName = "settings.properties";
            File settingsFile = new File(settingsFileName);
            if (settingsFile.exists())
            {
                SETTINGS.load(new FileInputStream(settingsFileName));
                Application.logger.fine("settings loaded.");
            }
            else
            {
                Application.logger.finest("Settings file [" + settingsFileName +
                                          "] does not exist in this directory");
            }
        }
        catch (IOException ex)
        {
            Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    static
    {
        //jdbc:mysql://localhost:3306/NanoSNFramework_Polls
        String host = SETTINGS.getProperty("DATABASE_HOST");
        String port = SETTINGS.getProperty("DATABASE_PORT");
        String databaseName = SETTINGS.getProperty("DATABASE_NAME");

        if (host == null || host.trim().equals(""))
        {
            host = "localhost";
        }

        if (port == null || port.trim().equals(""))
        {
            port = "3306";
        }
        
        DATABASE_CONNECTION_STRING = "jdbc:mysql://" + host + ":" + port + "/" + databaseName;
    }

    public static String getDatabaseConnectionString()
    {
        return DATABASE_CONNECTION_STRING;
    }

    public static String getDatabaseUserName()
    {
        return SETTINGS.getProperty("DATABASE_USER");
    }
    
    public static String getDatabasePassword()
    {
        return SETTINGS.getProperty("DATABASE_PASSWORD");
    }

    public static String getDatabaseEngine() throws Exception
    {
        throw new Exception("Not Yet Supported.");
    }

    public static String getPackagePrefix()
    {
        return SETTINGS.getProperty("PACKAGE_PREFIX");
    }
}
